package com.homelock.controller;

import java.util.*;
import java.util.stream.Collectors;

import com.homelock.vo.ZxLockInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.homelock.constants.DevUserRoleEnum;
import com.homelock.domain.LockIdentity;
import com.homelock.entity.HlDevInfo;
import com.homelock.entity.HlDevUser;
import com.homelock.service.HlDevInfoService;
import com.homelock.service.LockUserService;
import com.homelock.vo.DevLockInfoVO;
import com.pi.base.contants.math.MathNumbers;
import com.pi.base.dto.result.AppResult;
import com.pi.http.base.controller.BaseController;

@RequestMapping("/dev/lock")
@RestController
public class DevLockInfoController extends BaseController {

  @SuppressWarnings("unused")
  private static final Logger logger = LoggerFactory.getLogger(DevLockInfoController.class);

  private HlDevInfoService devInfoService;
  
  private LockUserService lockUserService;

  @Autowired
  public DevLockInfoController(HlDevInfoService devInfoService, LockUserService lockUserService) {
    Assert.notNull(devInfoService, "锁具服务为空");
    Assert.notNull(lockUserService, "锁具用户服务为空");
    this.devInfoService = devInfoService;
    this.lockUserService = lockUserService;
  }

  @RequestMapping("/listByUserId")
  public AppResult listByUserId() {
    List<HlDevUser> devUserList = lockUserService.queryUserLockList(getLoginUserId());
    List<DevLockInfoVO> devInfoVoList;
    if (!CollectionUtils.isEmpty(devUserList)) {
      devInfoVoList = new ArrayList<>(devUserList.size());
      List<Long> devIds = devUserList.stream().map(HlDevUser::getDevId).collect(Collectors.toList());
      List<HlDevInfo> devinfolist = devInfoService.getDevInfoByIds(devIds);
      Map<Long, HlDevInfo> devInfoMap;
      if(!CollectionUtils.isEmpty(devinfolist)) {
        devInfoMap = devinfolist.stream().collect(Collectors.toMap(HlDevInfo::getId, dev -> dev));
      }else {
        devInfoMap = new HashMap<>(0);
      }
      for (HlDevUser devUser : devUserList) {
        if(!devInfoMap.containsKey(devUser.getDevId())) {
          continue;
        }
        HlDevInfo devInfo = devInfoMap.get(devUser.getDevId());
        DevLockInfoVO vo = new DevLockInfoVO();
        vo.setDevId(devUser.getDevId().toString());
        vo.setDevName(devInfo.getDevName());
        vo.setDevCode(devInfo.getDevCode());
        vo.setDevBattery(null != devInfo.getDevBattery() ? devInfo.getDevBattery().toString() : MathNumbers.ZERO.toString());
        vo.setAdminFlag(DevUserRoleEnum.getAdminFlag(devUser.getDevUserRole().intValue()).toString());
        vo.setDevMode(devInfo.getDevMode());
        devInfoVoList.add(vo);
      }
    }else {
      devInfoVoList = new ArrayList<>(0);
    }
    return AppResult.newSuccessResult(devInfoVoList);
  }

  @RequestMapping("/add")
  public AppResult adddev(String devCode, String devName, String devImei, Long sourceId,Integer devType) {
    devInfoService.addDevInfo(devCode, devName, devImei, getLoginUserId(), sourceId,devType);
    return AppResult.OK;
  }

  @RequestMapping("/getByDevCode")
  public AppResult getByDevCode(String devCode) {
    HlDevInfo devinfo = devInfoService.getDevInfoBycode(devCode);
    return AppResult.newSuccessResult(devinfo);
  }

  @RequestMapping("/getByDevCode/v2")
  public AppResult getZxLockInfoByDevCode(String devCode) {
    ZxLockInfoVo devinfo = devInfoService.getZxDevInfoBycode(devCode);
    return AppResult.newSuccessResult(devinfo);
  }

  @RequestMapping("/getByDevid")
  public AppResult getByDevid(long id) {
    HlDevInfo devinfo = devInfoService.getDevInfobyId(id);
    return AppResult.newSuccessResult(devinfo);
  }

  @RequestMapping("/updateDevInfo")
  public AppResult updateDevInfo(String devCode, String devName, Long ownerId) {
    devInfoService.updateDevInfo(devCode, devName, ownerId);
    return AppResult.OK;
  }

  /**
   * 清空锁具
   * 
   * @param ownerId
   * @return
   */
  @RequestMapping("/remove")
  public AppResult removeDevInfo(Long devId) {
    devInfoService.removeDevInfo(devId);
    return AppResult.OK;
  }

  /*
   * 删除用户下面的锁具
   */
  @RequestMapping("/removeDevInfobyUserid")
  public AppResult removeDevInfo(String devCode, Long ownerId) {
    devInfoService.removeDevInfo(devCode, ownerId);
    return AppResult.OK;
  }
 
  @RequestMapping("/cipher/identityCode")
  public AppResult getCipherDevcode(Long devId){
      LockIdentity lockIdentity = devInfoService.getCipherIdentityCode(devId, getLoginUserId());
      return AppResult.newSuccessResult(lockIdentity);
  }
  
  @RequestMapping("/cipher/identityCode/v2")
  public AppResult getCipherDevcode(String devCode, String imei){
       imei = StringUtils.isBlank(imei) ? "0" : imei;
      LockIdentity lockIdentity = devInfoService.getCipherIdentityCode(devCode, imei, getLoginUserId());
      return AppResult.newSuccessResult(lockIdentity);
  }
  
  @RequestMapping("/mode")
  public AppResult updateLockMode(Long devId, Integer devMode){
      devInfoService.updateLockMode(devId, devMode);
      return AppResult.OK;
  }
}
